package com.ericsson.nfc.signing.bc.crypto.signers;

import java.math.BigInteger;
import java.util.Enumeration;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObject;
import org.bouncycastle.asn1.DERSequence;

/**
  * 
  * RFC 3279
  * 
  * The Dss-Sig-Value object.
  * <pre>
  *      Dss-Sig-Value ::= SEQUENCE {
        r INTEGER,
        s INTEGER }
  * </pre>
  */
public class DssSigValue
    extends ASN1Encodable
{
    private BigInteger              r;
    private BigInteger              s;

    public static DssSigValue getInstance(
        ASN1TaggedObject obj,
        boolean          explicit)
    {
        return getInstance(ASN1Sequence.getInstance(obj, explicit));
    }

    public static DssSigValue getInstance(
        Object  obj)
    {
        if (obj instanceof DssSigValue)
        {
            return (DssSigValue)obj;
        }
        else if (obj instanceof ASN1Sequence)
        {
            return new DssSigValue((ASN1Sequence)obj);
        }

        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
    }

    public DssSigValue(
        BigInteger r,
        BigInteger s)
    {
        this.r = r;
        this.s = s;
    }

    public DssSigValue(
        ASN1Sequence  obj)
    {
        Enumeration             e = obj.getObjects();

        r = DERInteger.getInstance(e.nextElement()).getValue();
        s = DERInteger.getInstance(e.nextElement()).getValue();
    }

    public BigInteger getR() {
        return r;
    }

    public BigInteger getS() {
        return s;
    }

    public DERObject toASN1Object()
    {
        ASN1EncodableVector  v = new ASN1EncodableVector();

        v.add(new DERInteger(r));
        v.add(new DERInteger(s));

        return new DERSequence(v);
    }
}
